package com.example.administrator.fangbook.read.presenter;

import android.os.Bundle;

import com.example.administrator.fangbook.HtlMvp.activity.BaseMvpPresenter;
import com.example.administrator.fangbook.dataLibrary.book.ShelfItemBook;
import com.example.administrator.fangbook.read.ReadActivity;
import com.example.administrator.fangbook.read.bean.ChineseallBookReadStartData;
import com.example.administrator.fangbook.read.bean.SOURCE_TYPE;
import com.example.administrator.fangbook.read.view.IReadView;

import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.ZLTxtFile;
import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
import org.geometerplus.zlibrary.text.view.ZLTextPosition;

import static com.example.administrator.fangbook.read.ReadActivity.mBookStartReadDataKey;

public class ReadPresenter extends BaseMvpPresenter<IReadView> {


    private ShelfItemBook mShelfBook;

    /**
     * 根据Intent传进来的数据转化成ZLFile
     *
     * @return
     */
    public ZLFile fromIntent() {
        ZLFile zFile = null;
        Bundle bundle = getView().getIntent().getExtras();
        if (bundle == null || !bundle.containsKey(mBookStartReadDataKey)) {
            return zFile;
        }
        ChineseallBookReadStartData readIntent = (ChineseallBookReadStartData) bundle.get(mBookStartReadDataKey);
        if (readIntent == null) {
            return zFile;
        }
        mShelfBook = readIntent.getShelfBook();
        if (readIntent.sourceType == SOURCE_TYPE.ST_LOCALFILE_TXT || readIntent.sourceType == SOURCE_TYPE.ST_LOCALFILE_TXT_DIRECTORY) {
            String chapterId = readIntent.getChapterId();
            ZLTxtFile m17KFile = new ZLTxtFile(readIntent.getBookId(), readIntent.getBookName(), chapterId, readIntent.getChapterName());
            m17KFile.setShelfBook(mShelfBook);
            ZLTextPosition startPosition = readIntent.getStartPosition();
            if (startPosition != null) {
                m17KFile.setStartPositioin(startPosition);
                m17KFile.setChapterId(startPosition.getTag());
            } else if (readIntent.sourceType == SOURCE_TYPE.ST_LOCALFILE_TXT_DIRECTORY) {
                //如果是目录进入指针都清空，从头开始
                m17KFile.setStartPositioin(new ZLTextFixedPosition(0, 0, 0));
            }
            zFile = m17KFile;
        } else if (readIntent.sourceType == SOURCE_TYPE.ST_LOCALFILE_EPUB || readIntent.sourceType == SOURCE_TYPE.ST_LOCALFILE_EPUB_DIRECTORY) {

        } else {

        }
        return zFile;
    }
}
